﻿#include"RBTree.hpp"

template<class K,class V>
class map
{
private:
	typedef RBTree<K, V> Container;
	typedef typename Container::Node Node;
public:
	typedef typename Container::iterator iterator;
	typedef typename Container::const_iterator const_iterator;

	map()
	{}

	~map()
	{}

	iterator begin()
	{
		return _con.begin();
	}

	iterator end()
	{
		return _con.end();
	}

	const_iterator cbegin()
	{
		return _con.cbegin();
	}

	const_iterator cend()
	{
		return _con.cend();
	}

	pair<iterator, bool> insert(const pair<K,V>& ins)
	{
		Node* tmp = _con.find(ins.first);
		if (tmp)
		{
			return make_pair(iterator(tmp), false);
		}
		else
		{
			_con.insert(ins.first,ins.second);
			Node* tmp = _con.find(ins.first);
			return make_pair(iterator(tmp), true);
		}
	}

	iterator find(const K& key)
	{
		Node* tmp = _con.find(key);
		return iterator(tmp);
	}

	size_t size()
	{
		return _con.size();
	}

	bool empty()
	{
		return _con.empty();
	}

	V& count(const K& key)
	{
		Node* tmp = find(key);
		if (!tmp)
		{
			return 0;
		}
		else
		{
			return 1;
		}
	}

	V& operator[](const K& key)
	{
		Node* tmp = _con.find(key);
		if (!tmp)
		{
			iterator ret = insert(make_pair(key, V())).first;
			return ret._Node->_val;
		}
		else
		{
			return tmp->_val;
		}
	}
private:
	Container _con;
};